<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
    require([ "dojo/_base/array", "dojo/_base/xhr", "dojo/request", "dojo/topic", "dojo/json" ],
            function(array, xhr, request, topic, JSON) {
                var deletItemsCount = 0;
                documentRestForm = function(formId) {
                    dijit.byId(formId).reset();
                    if (formId.indexOf('UFD') != -1) {
                        var rowid = jQuery("#documentGird").jqGrid('getGridParam', 'selrow');
                        var document = jQuery("#documentGird").jqGrid('getRowData', rowid);
                        dijit.byId('documentUFD').setValues(document);
                    }
                };
                topic.subscribe("document/insertOrUpdate", documentRestForm);
                var syncDB = function(url, jsonData) {
                    request.post(url, {
                        data : jsonData,
                        headers : {
                            'Content-Type' : "application/json; charset=utf-8"
                        },
                        timeout : 30000
                    }).then(function(response) {
                        topic.publish("notifyMsgTopic", {
                            message : response,
                            type : "message",
                            duration : 1000
                        });
                        var currentPage = jQuery('#documentGird').jqGrid('getGridParam', 'page');
                        var pageSize = jQuery('#documentGird').jqGrid('getGridParam', 'rowNum');
                        var totalCount = jQuery('#documentGird').jqGrid('getGridParam', 'records') - deletItemsCount;
                        var totalPages = Math.ceil(totalCount / pageSize);
                        jQuery("#documentGird").jqGrid('setGridParam', {
                            page : currentPage > totalPages ? totalPages : currentPage
                        }).trigger("reloadGrid");
                    }, function(error) {
                        alert(error);
                    });
                };
                documentSubmitForm = function(formId, type) {
                    var url = "document/insert";
                    if (type == 'u') {
                        url = "document/update";
                    }
                    var form = dijit.byId(formId);
                    if (form.isValid()) {
                        var json = JSON.stringify(form.getValues());
                        syncDB(url, json);
                        form.hide();
                        topic.publish("document/insertOrUpdate", formId);
                    } else {
                        form.validate();
                    }
                };
                documentDelete = function() {
                    var ids = jQuery("#documentGird").jqGrid('getGridParam', 'selarrrow') || [];
                    deletItemsCount = ids.length;
                    if (ids.length > 0) {
                        if (confirm('确认删除所选定的项？')) {
                            var document = {};
                            document.id = '';
                            array.forEach(ids, function(entry, i) {
                                document.id += entry + ",";
                            });
                            syncDB("document/delete", JSON.stringify(document));
                        }
                    } else {
                        alert('请选择至少一条数据!');
                    }
                };
                documentQuery = function(formId) {
                    var document = dijit.byId(formId).getValues();
                    jQuery("#documentGird").jqGrid('setGridParam', {
                        postData : document,
                        mtype : "post"
                    }).trigger('reloadGrid');
                };
            });
</script>
<div data-dojo-type="dijit/Dialog" id="documentIFD" title="新增">
  <div class="dijitDialogPaneContentArea">
    <table>
      <tr>
        <td><label for="d_recordid">记录ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="d_recordid"></td>
      </tr>
      <tr>
        <td><label for="d_subject">标题: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="d_subject"></td>
      </tr>
      <tr>
        <td><label for="d_author">作者: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="d_author"></td>
      </tr>
      <tr>
        <td><label for="d_filedate">文件创建日期: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="d_filedate"></td>
      </tr>
      <tr>
        <td><label for="d_filetype">文件类型: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="d_filetype"></td>
      </tr>
      <tr>
        <td><label for="ngsj">拟稿时间: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="ngsj"></td>
      </tr>
      <tr>
        <td><label for="ngrid">拟稿人ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="ngrid"></td>
      </tr>
      <tr>
        <td><label for="gwmj">公文密级：机密,秘密,内部,一般: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="gwmj"></td>
      </tr>
      <tr>
        <td><label for="gwbh">公文编号: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="gwbh"></td>
      </tr>
      <tr>
        <td><label for="gwbt">公文标题: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="gwbt"></td>
      </tr>
      <tr>
        <td><label for="gwlb">公文类别: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="gwlb"></td>
      </tr>
      <tr>
        <td><label for="gwlx">公文类型：0发文,1收文: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="gwlx"></td>
      </tr>
      <tr>
        <td><label for="scbs">删除标识：0未删除（默认值）、1已删除: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="scbs"></td>
      </tr>
      <tr>
        <td><label for="state">当前状态：0:未提交审批,1:已提交审批,2:审批中,3:退回,4:撤消,5:终止: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="state"></td>
      </tr>
      <tr>
        <td><label for="scrid">删除人ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="scrid"></td>
      </tr>
      <tr>
        <td><label for="scsj">删除时间: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="scsj"></td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="documentSubmitForm('documentIFD','i')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="documentRestForm('documentIFD');">重置</button>
  </div>
</div>
<div data-dojo-type="dijit/Dialog" id="documentUFD" title="更新">
  <div class="dijitDialogPaneContentArea">
    <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, readOnly: true" type="text"
      name="d_id" style="display: none;">
    <table>
      <tr>
        <td><label for="d_recordid">记录ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="d_recordid"></td>
      </tr>
      <tr>
        <td><label for="d_subject">标题: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="d_subject"></td>
      </tr>
      <tr>
        <td><label for="d_author">作者: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="d_author"></td>
      </tr>
      <tr>
        <td><label for="d_filedate">文件创建日期: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="d_filedate"></td>
      </tr>
      <tr>
        <td><label for="d_filetype">文件类型: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="d_filetype"></td>
      </tr>
      <tr>
        <td><label for="ngsj">拟稿时间: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="ngsj"></td>
      </tr>
      <tr>
        <td><label for="ngrid">拟稿人ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="ngrid"></td>
      </tr>
      <tr>
        <td><label for="gwmj">公文密级：机密,秘密,内部,一般: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="gwmj"></td>
      </tr>
      <tr>
        <td><label for="gwbh">公文编号: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="gwbh"></td>
      </tr>
      <tr>
        <td><label for="gwbt">公文标题: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="gwbt"></td>
      </tr>
      <tr>
        <td><label for="gwlb">公文类别: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="gwlb"></td>
      </tr>
      <tr>
        <td><label for="gwlx">公文类型：0发文,1收文: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="gwlx"></td>
      </tr>
      <tr>
        <td><label for="scbs">删除标识：0未删除（默认值）、1已删除: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="scbs"></td>
      </tr>
      <tr>
        <td><label for="state">当前状态：0:未提交审批,1:已提交审批,2:审批中,3:退回,4:撤消,5:终止: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="state"></td>
      </tr>
      <tr>
        <td><label for="scrid">删除人ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="scrid"></td>
      </tr>
      <tr>
        <td><label for="scsj">删除时间: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="scsj"></td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="documentSubmitForm('documentUFD','u')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="documentRestForm('documentUFD');">重置</button>
  </div>
</div>
<fieldset>
  <legend>&nbsp;&nbsp;信息选项&nbsp;&nbsp;</legend>
  <form id="queryDocumentForm" data-dojo-type="dijit/form/Form" method="post">
    <div style="margin-left: 5px;">
      <table>
        <tr>
          <td><label for="d_recordid">记录ID: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="d_recordid"></td>
        </tr>
        <tr>
          <td><label for="d_subject">标题: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="d_subject"></td>
        </tr>
        <tr>
          <td><label for="d_author">作者: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="d_author"></td>
        </tr>
        <tr>
          <td><label for="d_filedate">文件创建日期: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="d_filedate"></td>
        </tr>
        <tr>
          <td><label for="d_filetype">文件类型: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="d_filetype"></td>
        </tr>
        <tr>
          <td><label for="ngsj">拟稿时间: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="ngsj"></td>
        </tr>
        <tr>
          <td><label for="ngrid">拟稿人ID: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="ngrid"></td>
        </tr>
        <tr>
          <td><label for="gwmj">公文密级：机密,秘密,内部,一般: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="gwmj"></td>
        </tr>
        <tr>
          <td><label for="gwbh">公文编号: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="gwbh"></td>
        </tr>
        <tr>
          <td><label for="gwbt">公文标题: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="gwbt"></td>
        </tr>
        <tr>
          <td><label for="gwlb">公文类别: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="gwlb"></td>
        </tr>
        <tr>
          <td><label for="gwlx">公文类型：0发文,1收文: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="gwlx"></td>
        </tr>
        <tr>
          <td><label for="scbs">删除标识：0未删除（默认值）、1已删除: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="scbs"></td>
        </tr>
        <tr>
          <td><label for="state">当前状态：0:未提交审批,1:已提交审批,2:审批中,3:退回,4:撤消,5:终止: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="state"></td>
        </tr>
        <tr>
          <td><label for="scrid">删除人ID: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="scrid"></td>
        </tr>
        <tr>
          <td><label for="scsj">删除时间: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="scsj"></td>
        </tr>
      </table>
    </div>
  </form>
  <div style="padding: 5px;">
    <button id="documentInsertBtn" data-dojo-type="dijit/form/Button" type="submit" label="新增">
      <script type="dojo/method" event="onClick" args="evt">
        dijit.byId('documentIFD').show();
      </script>
    </button>
    <button id="documentDeleteBtn" data-dojo-type="dijit/form/Button" type="button" label="删除" onclick="documentDelete();"></button>
    <button id="documentUpdateBtn" data-dojo-type="dijit/form/Button" type="button" label="更新">
      <script type="dojo/method" event="onClick" args="evt">
        var ids = jQuery("#documentGird").jqGrid('getGridParam', 'selarrrow') || [];
        if (ids.length != 1) {
          alert('每次只能修改一条数据!');
        }else{
          var document = jQuery("#documentGird").jqGrid('getRowData', ids[0]);
          dijit.byId('documentUFD').setValues(document);
          dijit.byId('documentUFD').show();
        }
      </script>
    </button>
    <button id="documentQueryBtn" data-dojo-type="dijit/form/Button" type="button" label="查询"
      onclick="documentQuery('queryDocumentForm');"></button>
  </div>
</fieldset>
<div id="gird">
  <table id="documentGird"></table>
  <div id="documentToolbar"></div>
</div>
<script type="text/javascript">
    jQuery("#documentGird").jqGrid({
        url : 'document/queryAsList',
        datatype : "json",
        autowidth : true,
        rownumbers : true,
        height : 'auto',
        colNames : [
          '主键ID',
          '记录ID',
          '标题',
          '作者',
          '文件创建日期',
          '文件类型',
          '拟稿时间',
          '拟稿人ID',
          '公文密级：机密,秘密,内部,一般',
          '公文编号',
          '公文标题',
          '公文类别',
          '公文类型：0发文,1收文',
          '删除标识：0未删除（默认值）、1已删除',
          '当前状态：0:未提交审批,1:已提交审批,2:审批中,3:退回,4:撤消,5:终止',
          '删除人ID',
          '删除时间'
        ],
        colModel : [ 
        {
          name : 'd_id',
          index : 'd_id',
          width : 60,
          key : true,
          hidden : true,
          align : 'center'
        },
        {
          name : 'd_recordid',
          index : 'd_recordid',
          align : 'center'
        },
        {
          name : 'd_subject',
          index : 'd_subject',
          align : 'center'
        },
        {
          name : 'd_author',
          index : 'd_author',
          align : 'center'
        },
        {
          name : 'd_filedate',
          index : 'd_filedate',
          align : 'center'
        },
        {
          name : 'd_filetype',
          index : 'd_filetype',
          align : 'center'
        },
        {
          name : 'ngsj',
          index : 'ngsj',
          align : 'center'
        },
        {
          name : 'ngrid',
          index : 'ngrid',
          align : 'center'
        },
        {
          name : 'gwmj',
          index : 'gwmj',
          align : 'center'
        },
        {
          name : 'gwbh',
          index : 'gwbh',
          align : 'center'
        },
        {
          name : 'gwbt',
          index : 'gwbt',
          align : 'center'
        },
        {
          name : 'gwlb',
          index : 'gwlb',
          align : 'center'
        },
        {
          name : 'gwlx',
          index : 'gwlx',
          align : 'center'
        },
        {
          name : 'scbs',
          index : 'scbs',
          align : 'center'
        },
        {
          name : 'state',
          index : 'state',
          align : 'center'
        },
        {
          name : 'scrid',
          index : 'scrid',
          align : 'center'
        },
        {
          name : 'scsj',
          index : 'scsj',
          align : 'center'
        }
        ],
        rowNum : 15,
        rowList : [ 15, 20, 25, 30 ],
        pager : '#documentToolbar',
        // sortname : 'username',
        recordpos : 'right',
        viewrecords : true,
        sortorder : "desc",
        jsonReader : {
            repeatitems : false
        },
        multiselect : true,
        shrinkToFit : false,
        caption : "eweboffice_t_document列表"
    });
    jQuery("#documentGird").jqGrid('navGrid', '#documentToolbar', {
        edit : false,
        add : false,
        del : false,
        search : false,
        refresh : true
    });
</script>
